<template>
  <div class="rubbish_box">
    <div id="slogan">
      <p class="tag">垃圾识别</p>
      <p>参与垃圾分类，保护地球家园，共创美好世界。</p>
    </div>
    <div class="rubbish_box_img" style="margin-top: 15px">
      <!-- <img src="https://img1.baidu.com/it/u=1785076211,2604031447&fm=253&fmt=auto&app=138&f=JPEG?w=334&h=222"   /> -->
      <img
        src="https://www.terracycle.cn/assets/about/index/free_recycling_programs-b8fdc32cfa82ad80145ef794e6d3ccdfc29de415333d9714b044f818d029bb48.jpg"
      />
      <div>
        <p>上传图片开始识别</p>
        <el-upload
          class="avatar-uploader"
          action="http://124.220.76.151:8003/rubbish-server/rubbish/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :data="{
            userId: $store.getters.userInfo.id,
            category: '1',
          }"
          accept="image/png, image/jpeg"
        >
          <i class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </div>
    </div>
    <div>查询历史</div>
    <div>
      <el-table :data="list">
        <el-table-column property="category" label="结果"></el-table-column>
        <el-table-column property="rubbish" label="图片识别"></el-table-column>
        <el-table-column property="rubbish" label="识别图片">
          <template slot-scope="scope">
            <el-image 
              style="width: 150px; height: 150px"
              :src="scope.row.imageUrl && scope.row.imageUrl.indexOf('http') != -1 ? scope.row.imageUrl : `http://124.220.76.151:8003/${scope.row.imageUrl}`" 
              :preview-src-list="[scope.row.imageUrl && scope.row.imageUrl.indexOf('http') != -1 ? scope.row.imageUrl : `http://124.220.76.151:8003/${scope.row.imageUrl}`]">
            </el-image>
            </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page.sync="query.current"
          :page-size="query.size"
          layout="total, prev, pager, next"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>

    <el-dialog title="识别结果" :visible.sync="dialogTableVisible">
      <el-result :title="rubbish.category" :subTitle="rubbish.rubbish">
        <template slot="icon">
          <el-image style="height: 300px" :src="avatar"></el-image>
        </template>
        <template slot="extra">
          <el-button
            type="primary"
            size="medium"
            @click="dialogTableVisible = false"
            >返回</el-button
          >
        </template>
      </el-result>
    </el-dialog>
  </div>
</template>
<script>
import { rubbishQuery } from "@/api/info";
export default {
  data() {
    return {
      list: [],
      rubbish: {},
      avatar: "",
      dialogTableVisible: false,
      query: {
        size: 10,
        current: 1,
        input: {
          userId: this.$store.getters.userInfo.id,
        },
      },
      total: 0,
    };
  },
  mounted() {
    this.queryList();
  },
  methods: {
    queryList() {
      rubbishQuery(this.query).then((res) => {
        this.list = res.data.records || [];
        this.total = res.data.total;
      });
    },
    handleAvatarSuccess(res, file, fileList) {
      this.avatar = URL.createObjectURL(file.raw);
      this.rubbish = res.data || {};
      this.dialogTableVisible = true;
      this.queryList()
    },
    beforeAvatarUpload(file) {
      console.log(file);
      this.rubbish = {};
      // const isJPG = file.type === "image/jpeg";
      const isLt3M = file.size / 1024 / 1024 < 3;

      // if (!isJPG) {
      //   this.$message.error("上传头像图片只能是 JPG 格式!");
      // }
      if (!isLt3M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      // return isJPG && isLt3M;
      return isLt3M;
    },
    handleCurrentChange(val) {
      this.query.current = val
      this.queryList()
    },
  },
};
</script>

<style lang="scss" scoped>
.rubbish_box {
  > div {
    width: 80vw;
    margin: 15px auto;
  }
  &_img {
    display: flex;
    img {
      width: 40vw;
      margin-right: 15px;
    }
  }
  .pagination{
    text-align: right;
  }
}
.avatar-uploader{
  border: 1px dashed #b7b1b1;
}
.avatar-uploader .el-upload {
  
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
  border: 1px solid #8c939d;
}
</style>
